<style lang="scss" scoped>
.add_site {
  li {
    height: 0.55rem;
    line-height: 0.55rem;
    div {
      width: 0.8rem;
    }
  }
  .site {
    .tit {
      width: 0.8rem;
    }
  }
  footer {
    padding-top: 0.85rem;
  }
}
</style>

<template>
  <div class="add_site full bgfff">
    <ul class="padding-lr15">
      <li class="flex-col border-b">
        <div>收货人：</div>
        <input type="text" class="box1" v-model="siteData.name" placeholder="请填写姓名">
      </li>
      <li class="flex-col border-b">
        <div>联系方式：</div>
        <input type="tel" class="box1" v-model="siteData.tel" placeholder="请填写手机号">
      </li>
      <li class="flex-col border-b">
        <div>所在省份：</div>
        <!-- <input type="text" class="box1" v-model="siteData.provinceid" placeholder="请选择省份"> -->
        <select @change="changeSiteInfo('siteData',1)" v-model="siteData.provinceid" class="box1">
           <option value="请选择">请选择</option>
          <option v-for="(it,i) in provinceList" :key="i" :value="it.provinceid">{{it.provinceName}}</option>
        </select>
        <i class="iconfont icon-jiantou"></i>
      </li>
      <li class="flex-col border-b">
        <div>所在城市：</div>
        <!-- <input type="text" class="box1" v-model="siteData.cityid" placeholder="请选择城市"> -->
        <select @change="changeSiteInfo('siteData',2)" v-model="siteData.cityid" class="box1">
          <option value="请选择">请选择</option>
          <option v-for="(it,i) in cityList" :key="i" :value="it.cityid">{{it.cityname}}</option>
        </select>
        <i class="iconfont icon-jiantou"></i>
      </li>
      <li class="flex-col border-b">
        <div>所在区县：</div>
        <!-- <input type="text" class="box1" v-model="siteData.districtid" placeholder="请选择区县"> -->
        <select name="" id="" placeholder="" @change="changeSiteInfo('siteData')" v-model="siteData.districtid" class="box1">
           <option value="请选择">请选择</option>
          <option v-for="(it,i) in districtList" :key="i" :value="it.districtid">{{it.districtname}}</option>
        </select>
        <i class="iconfont icon-jiantou"></i>
      </li>
    </ul>
    <div class="site flex-col border-b padding-15">
      <div class="tit">详细地址：</div>
      <textarea class="box1 padding-r20" v-model="siteData.detailed" placeholder="详细地址需填写楼栋楼层或房间号信息解决"></textarea>
      <div class="padding-l20">
        <!-- <i class="iconfont icon-jiantou"></i> -->
      </div>
    </div>
    <footer class="box-center footer font-14 box-center c82">
      <div class="cfff box-center margin-b20" @click="handelSite">保存</div>
    </footer>
  </div>
</template>

<script>
export default {
  name: "add_site",
  data() {
    return {
      siteData: {
        name: "",
        tel: "",
        provinceid: "",
        cityid: "",
        districtid: "",
        detailed: "",
        isdefault: 1
      },
    };
  },
  mounted() {
    let that = this;
    this.$nextTick(() => {
      if (that.$route.query.id) this.getSite(that.$route.query.id); //修改
      that.getProvince();
    });
  },
  methods: {
    /**@augments
     * 添加收货地址
     * has = true 为修改地址
     */
    addSite(has = false) {
      let that = this,
        _data = this.siteData;
      let ops = {
        name: _data.name,
        tel: _data.tel,
        provinceid: _data.provinceid,
        cityid: _data.cityid,
        districtid: _data.districtid,
        detailed: _data.detailed,
        provinceName: _data.provinceName,
        cityName: _data.cityName,
        districtName: _data.districtName,
        isdefault: _data.isdefault //是否默认 0=默认 1=非默认
        //thingCode:'' // 修改地址=id
      };
      // 是否默认（添加第一条，默认）
      if (that.$route.query.has) {
        ops["isdefault"] = 0;
      }
      // 修改收获地址
      if (has) {
        ops["thingCode"] = that.$route.query.id;
      }
      this.http
        .post("address/save", ops)
        .then(res => {
          if (has) that.layer("修改成功", "m");
          else that.layer("添加成功", "m");
          setTimeout(() => {
            that.$router.back(-1);
          }, 1000);
        })
        .catch(err => {});
    },
    // 添加，修改地址
    handelSite() {
      let has = false; //添加
      if (this.$route.query.id) has = true; //修改
      this.addSite(has);
    },
    //修改获取原来的地址
    getSite(id) {
      let that = this;
      this.http
        .get("address/info/" + id)
        .then(res => {
          that.siteData = res.data;
          that.getCity(that.siteData.provinceid);
          that.getDistrict(that.siteData.cityid);
        })
        .catch(err => {});
    },
    
  }
};
</script>


